﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Xml;
using System.Linq;
using System.Collections.Generic;

namespace SilverlightBase.Classes
{
	public class Images
	{
		public static int GetImageNodeId(string xmlDoc, string nodeAlias)
		{
			TextReader textReader = new StringReader(xmlDoc);
			XElement siteXmlDoc = XElement.Load(textReader);

			var images = from node in siteXmlDoc.Descendants()
						 where (string)node.Attribute("alias") == nodeAlias
						 select node.Value;

			int i = 0;

			foreach (string image in images)
			{
				i = image.ToInt32();
			}

			textReader.Close();

			return i;
		}

		public static void SetupDefaultImages(string siteXml, Dictionary<int, string> siteImages)
		{
			Uri uri = null;
			// Setup images
			try
			{
				uri = new Uri(siteImages[Images.GetImageNodeId(siteXml, "silverlightLogoImage")], UriKind.Absolute);
				((Page)Application.Current.RootVisual).Logo.Source = new System.Windows.Media.Imaging.BitmapImage(uri);
			}
			catch
			{
			}

			try
			{
				uri = new Uri(siteImages[Images.GetImageNodeId(siteXml, "silverlightBackgroundImage")], UriKind.Absolute);
				((Page)Application.Current.RootVisual).Bgr.Source = new System.Windows.Media.Imaging.BitmapImage(uri);
			}
			catch
			{
			}
		}
	}
}
